<?php

class Confirm_sheet extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Confirm_sheet':
	 * @var integer $id
	 * @var integer $loading_status
	 * @var double $mileage
	 * @var string $amount_kind
	 * @var double $advance_payment
	 * @var double $balance
	 * @var double $total_amount
	 * @var string $remark
	 * @var double $gas_fee
	 * @var double $bridge_charge
	 * @var string $origin
	 * @var string $destination
	 * @var string $fuel_name
	 * @var double $extra_cost
	 * @var double $total_cost
	 * @var integer $state
	 * @var string $create_time
	 */
	
	//查询用临时变量
	public $consignor_name;
	public $consignee_name;
	public $goods_name;
	public $goods_demand_deliver_time;
	//查询用临时变量
	
	public $truck_type;//车头车型
	public $truck_type_ratio;//车头车型绩效计算比率
	
	private $_loading_status=array('0'=>'重驶','1'=>'指定空驶');
	//private $_fuel_name=array('plain_empty'=>'平原车头空驶油耗','ghat_empty'=>'山路车头空驶油耗','plain_min'=>'平原空驶或载货10吨以下油耗',
	//'ghat_min'=>'山路空驶或载货10吨以下油耗','plain_mid'=>'平原轻驶油耗','ghat_mid'=>'山路轻驶油耗','plain_max'=>'平原重驶油耗','ghat_max'=>'山路重驶油耗');
	private $_amount_kind=array('0'=>'现金','1'=>'转账','2'=>'其他');
	private $_return_paper=array('0'=>'是','1'=>'否');

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'confirm_sheet';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('remark','length','max'=>200),
			array('prepaid_place,repayment_place','length','max'=>50),
			array('loading_status', 'numerical', 'integerOnly'=>true),
			array('mileage,driver_prepaid,driver_repayment,driver_salary,total_cost,bridge_charge', 'numerical'),
			array('truck_plate_no,driver1_name,driver1_mobile,type,mileage,driving_circuit,driver_salary,return_paper,
			total_cost,bridge_charge,compute_ratio,driver_repayment', 'required'),
			array('mileage', 'match','pattern' => '/^\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位'),
			array('driver_prepaid,driver_repayment,driver_salary,total_cost,bridge_charge', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('compute_ratio', 'numerical','integerOnly'=>true,'min'=>0,'max'=>1000),
			
			//重驶与制定空驶验证 生成客户端验证
			array('driving_circuit', 'numerical', 'integerOnly'=>true,'on'=>'all'),
			array('special_reasons','length','max'=>100,'on'=>'all'),
			array('advance_payment,balance,total_amount', 'numerical','on'=>'all'),
			array('amount_kind,advance_payment,balance,total_amount', 'required','on'=>'all'),
			array('advance_payment,balance,total_amount', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'all'),
			array('origin','length','max'=>50,'on'=>'all'),
			array('destination','length','max'=>50,'on'=>'all'),
			array('fuel_name','length','max'=>20,'on'=>'all'),
			array('gas_fee,gas_amount,extra_cost,total_profit,front_mileage,empty_mileage,light_mileage,heavy_mileage
				  actual_front_mileage,actual_empty_mileage,actual_light_mileage,actual_heavy_mileage,charge_off_sum,
				  front_fuel,empty_fuel,light_fuel,heavy_fuel,actual_front_fuel,actual_empty_fuel,damage_cost,
				  actual_light_fuel,actual_heavy_fuel,front_cost,empty_cost,light_cost,heavy_cost,amerce_cost', 'numerical','on'=>'all'),
			array('gas_amount,front_mileage,empty_mileage,light_mileage,heavy_mileage,actual_front_mileage,actual_empty_mileage,
					actual_light_mileage,actual_heavy_mileage', 'match','pattern' => '/^\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位','on'=>'all'),
			array('gas_fee, extra_cost, total_profit,front_cost,empty_cost,light_cost,heavy_cost,amerce_cost,damage_cost,charge_off_sum', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'all'),
			array('front_fuel,empty_fuel,light_fuel,heavy_fuel,actual_front_fuel,actual_empty_fuel,
				  actual_light_fuel,actual_heavy_fuel', 'match','pattern' => '/^\d{1,3}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多3位，小数位2位','on'=>'all'),
			array('gas_amount,gas_fee,origin,destination,fuel_name,extra_cost,total_profit,front_mileage,empty_mileage,
					light_mileage,heavy_mileage,actual_front_mileage,actual_empty_mileage,actual_light_mileage,damage_cost,
					actual_heavy_mileage,front_fuel,empty_fuel,light_fuel,heavy_fuel,actual_front_fuel,actual_empty_fuel,charge_off_sum,
				  actual_light_fuel,actual_heavy_fuel,front_cost,empty_cost,light_cost,heavy_cost,amerce_cost', 'required','on'=>'all'),
			
			//重驶验证
			array('special_reasons','length','max'=>100,'on'=>'heaven'),
			array('advance_payment,balance,total_amount,front_mileage,empty_mileage,light_mileage,heavy_mileage
				  actual_front_mileage,actual_empty_mileage,actual_light_mileage,actual_heavy_mileage,
				  front_fuel,empty_fuel,light_fuel,heavy_fuel,actual_front_fuel,actual_empty_fuel,
				  actual_light_fuel,actual_heavy_fuel,amerce_cost,damage_cost,charge_off_sum,
				  front_cost,empty_cost,light_cost,heavy_cost,total_profit', 'numerical','on'=>'heaven'),
			array('front_mileage,empty_mileage,light_mileage,heavy_mileage,actual_front_mileage,actual_empty_mileage,
					actual_light_mileage,actual_heavy_mileage', 'match','pattern' => '/^\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位','on'=>'heaven'),
			array('advance_payment,balance,total_amount,front_cost,empty_cost,light_cost,heavy_cost,amerce_cost,damage_cost,charge_off_sum,total_profit', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'heaven'),
			array('front_fuel,empty_fuel,light_fuel,heavy_fuel,actual_front_fuel,actual_empty_fuel,
				  actual_light_fuel,actual_heavy_fuel', 'match','pattern' => '/^\d{1,3}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多3位，小数位2位','on'=>'heaven'),
			array('amount_kind,advance_payment,balance,total_amount,front_mileage,empty_mileage,light_mileage,heavy_mileage
				  actual_front_mileage,actual_empty_mileage,actual_light_mileage,actual_heavy_mileage,
				  front_fuel,empty_fuel,light_fuel,heavy_fuel,actual_front_fuel,actual_empty_fuel,damage_cost,
				  actual_light_fuel,actual_heavy_fuel,front_cost,empty_cost,light_cost,heavy_cost,amerce_cost,charge_off_sum,total_profit', 'required','on'=>'heaven'),
			
			//指定空驶验证
			array('driving_circuit', 'numerical', 'integerOnly'=>true,'on'=>'empty'),
			array('origin','length','max'=>50,'on'=>'empty'),
			array('destination','length','max'=>50,'on'=>'empty'),
			array('fuel_name','length','max'=>20,'on'=>'empty'),
			array('gas_fee,gas_amount,extra_cost', 'numerical','on'=>'empty'),
			array('gas_fee,gas_amount,origin,destination,fuel_name,extra_cost', 'required','on'=>'empty'),
			array('gas_fee, extra_cost', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'empty'),
			array('gas_amount', 'match','pattern' => '/^-?\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位','on'=>'empty'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
			$this->create_user=Yii::app()->session['true_name'];
		}
		$this->update_time=date('Y-m-d G:i:s');
		$this->update_user=Yii::app()->session['true_name'];
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()) return;
		$this->compute_ratio=$this->compute_ratio/100;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			//查询用临时变量
			'consignor_name'=>'货主(发货人)1',
			'consignee_name'=>'客户(收货人)1',
			'goods_name'=>'货物名称1',
			'goods_demand_deliver_time'=>'提货时间', 
			//查询用临时变量
			'id' => '索引号',
			'confirm_no' => '派车单号',
			'truck_plate_no' =>'车牌号',
			'board_plate_no' =>'尾板牌号',
			'type' => '车型',
			'compute_ratio' =>'绩效比率(%)',
			'driver1_name' =>'司机1姓名',
			'driver2_name' =>'司机2姓名',
			'driver1_mobile' =>'司机1手机',
			'driver2_mobile' =>'司机2手机',
			'loading_status' => '行驶状态',//0重驶 1指定空驶
			'front_mileage'=>'车头空驶总里程',
			'empty_mileage'=>'空驶总里程',
			'light_mileage'=>'轻驶总里程',
			'heavy_mileage'=>'重驶总里程',
			'actual_front_mileage'=>'实际车头空驶总里程',
			'actual_empty_mileage'=>'实际空驶总里程',
			'actual_light_mileage'=>'实际轻驶总里程',
			'actual_heavy_mileage'=>'实际重驶总里程',
			'front_fuel'=>'车头空驶标准油耗',
			'empty_fuel'=>'空驶标准油耗',
			'light_fuel'=>'轻驶标准油耗',
			'heavy_fuel'=>'重驶标准油耗',
			'actual_front_fuel'=>'实际车头空驶油耗',
			'actual_empty_fuel'=>'实际空驶油耗',
			'actual_light_fuel'=>'实际轻驶油耗',
			'actual_heavy_fuel'=>'实际重驶油耗',
			'front_cost'=>'车头空驶费用',
			'empty_cost'=>'空驶费用',
			'light_cost'=>'轻驶费用',
			'heavy_cost'=>'重驶费用',
			'mileage' => '运输总里程',
			'driving_circuit' => '行驶线路', //0高速 1国道 2辅路
			'amount_kind' => '收入类型',
			'special_reasons' => '特殊原因',
			'advance_payment' => '预付款',
			'balance' => '尾款',
			'total_amount' => '收入总额',
			'remark' => '备注',
			'gas_fee' => '油费',
			'gas_amount' => '油量(升)',
			'bridge_charge' => '路桥费',
			'origin' => '始发地',
			'destination' => '目的地',
			'fuel_name' => '标准油耗', //对应标准油耗表内8种状况之一 plain_empty、ghat_empty、plain_min.....
			'extra_cost' => '特殊费用',
			'amerce_cost'=>'罚款',
			'driver_salary' => '司机绩效',
			'damage_cost' => '货损货差额',
			'charge_off_sum' => '核销金额',
			'total_cost' => '费用总额',
			'total_profit' => '利润总额',
			'state' => '状态',
			'driver_prepaid' => '司机预支费用',
			'driver_repayment' => '司机还款额',
			'prepaid_place' => '预支地点',
			'repayment_place' => '还款地点',
			'return_paper' => '是否回单',
			'create_user' => '确认人',
			'update_user' => '修改人',
			'update_time' => '最后修改时间',
			'create_time' => '确认时间',
			'appointment_time' => '业务时间'
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'confirm_no'=>array('B',12),
			'loading_status'=>array('C',12,'getLoadingStatusName'),
			'truck_plate_no'=>array('D',12),
			'board_plate_no'=>array('E',12,'format'=>'0'),
			'type'=>array('F',12),
			'driver1_name'=>array('G',12),
			'consignor_name'=>array('H',12),
			'consignee_name'=>array('I',12),
			'goods_name'=>array('J',12),
			'origin'=>array('K',12),
			'destination'=>array('L',12),
			'goods_demand_deliver_time'=>array('M',12,'getGoodsDemandDeliverTime'),
			'appointment_time'=>array('N',12),
			'create_time'=>array('O',20),
		);
	}
	
	public function getGoodsDemandDeliverTime(){
		return $this->goods_demand_deliver_time!=''?substr($this->goods_demand_deliver_time,0,10):'';
	}
	
	public function getLoadingStatusNames(){
		return $this->_loading_status;
	} 
	
	public function getLoadingStatusName($n=null){
		return $this->_loading_status[$n==null?$this->loading_status:$n];
	}
	
	public function getDrivingCircuitNames(){
		return Yii::app()->params['driving_circuit'];
	} 
	
	public function getDrivingCircuitName($n=null){
		return  Yii::app()->params['driving_circuit'][$n==null?$this->driving_circuit:$n];
	}
	
	public function getFuelNames(){
		return Yii::app()->params['fuel_consumption'];
	}
	
	public function getFuelName($n=null){
		return Yii::app()->params['fuel_consumption'][$n==null?$this->fuel_name:$n];
	}
	
	public function getAmountKindNames(){
		return $this->_amount_kind;
	}
	
	public function getAmountKindName($n=null){
		return $this->_amount_kind[$n==null?$this->amount_kind:$n];
	}
	
	public function getReturnPaperNames(){
		return $this->_return_paper;
	}
	
	public function getReturnPaperName($n=null){
		return $this->_return_paper[$n==null?$this->return_paper:$n];
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		//$params[]='board_reg_no';
		
		return $params;
	}
}